package com.taotao.web.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.taotao.common.service.ApiHttpClientService;
import com.taotao.common.service.RedisService;
import com.taotao.pojo.TbItemDesc;
import com.taotao.web.bean.TbItem;

@Service
public class ItemService {

	@Value("${TAOTAO_MANAGE_URL}")
	private String TAOTAO_MANAGE_URL;
	
    public static final String REDIS_KEY = "TAOTAO_WEB_ITEM_DETAIL_";

    private static final Integer REDIS_TIME = 60 * 60 * 24;

	private static final ObjectMapper MAPPER = new ObjectMapper();
	@Autowired
	private ApiHttpClientService httpClientService;
	
	@Autowired
	private RedisService redisService;
	
	public TbItem queryById(long itemId) {
		  // 从缓存中命中
        String key = REDIS_KEY + itemId;
        try {
            String cacheData = this.redisService.get(key);
            if (StringUtils.isNotEmpty(cacheData)) {
                // 命中
                return MAPPER.readValue(cacheData, TbItem.class);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        String url = TAOTAO_MANAGE_URL + "/rest/api/item/" + itemId;
        try {
            String jsonData = this.httpClientService.doGet(url);
            if (StringUtils.isEmpty(jsonData)) {
                return null;
            }

            try {
                // 将结果写入到缓存中
                this.redisService.set(key, jsonData, REDIS_TIME);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return MAPPER.readValue(jsonData, TbItem.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
	}

	public TbItemDesc queryItemDescByItemId(Long itemId) {
		String url = TAOTAO_MANAGE_URL + "/rest/api/item/desc/" + itemId;
		try {
			String jsonData = this.httpClientService.doGet(url);
			if (StringUtils.isEmpty(jsonData)) {
				return null;
			}
			return MAPPER.readValue(jsonData, TbItemDesc.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String queryItemParamByItemId(Long itemId) {
		try {
			String url = TAOTAO_MANAGE_URL + "/rest/api/item/param/item/" + itemId;
			String jsonData = this.httpClientService.doGet(url);
			// 解析JSON
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			ArrayNode paramData = (ArrayNode) MAPPER.readTree(jsonNode.get("paramData").asText());
			StringBuilder sb = new StringBuilder();
			sb.append(
					"<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\"><tbody>");
			for (JsonNode param : paramData) {
				sb.append("<tr><th class=\"tdTitle\" colspan=\"2\">" + param.get("group").asText() + "</th></tr>");
				ArrayNode params = (ArrayNode) param.get("params");
				for (JsonNode p : params) {
					sb.append("<tr><td class=\"tdTitle\">" + p.get("k").asText() + "</td><td>" + p.get("v").asText()
							+ "</td></tr>");
				}
			}
			sb.append("</tbody></table>");
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
